package com.shopping.service.service.simple.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.page.PageMethod;
import com.shopping.common.core.constant.ErrorCodeEnum;
import com.shopping.common.core.object.ResponseResult;
import com.shopping.common.core.object.TokenData;
import com.shopping.common.core.util.MyModelUtil;
import com.shopping.common.sp.dto.simple.SimpleDto;
import com.shopping.common.sp.query.simple.SimpleQuery;
import com.shopping.common.sp.vo.simple.SimpleVo;
import com.shopping.service.mapper.SimpleMapper;
import com.shopping.common.sp.model.simple.Simple;
import com.shopping.service.service.simple.SimpleService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class SimpleServiceImpl implements SimpleService {

    @Autowired
    private SimpleMapper simpleMapper;

    @Override
    public ResponseResult<Void> addSimple(SimpleDto dto) {
        //值转移
        Simple simple = new Simple();
        BeanUtils.copyProperties(dto,simple);
        Long userId = TokenData.takeFormRequest().getUserId();
        String userName = TokenData.takeFormRequest().getUserName();
        //设置表必须的字段
        simple.setCreateUserId(userId);
        simple.setCreateUserName(userName);
        simple.setCreateTime(new Date());

        simple.setUpdateUserId(userId);
        simple.setUpdateUserName(userName);
        simple.setUpdateTime(new Date());

        simpleMapper.insert(simple);
        return ResponseResult.success();
    }

    @Override
    public ResponseResult<Page<SimpleVo>> simplePage(SimpleQuery simpleQuery) {
        Integer pageNum = simpleQuery.getPageIndex();
        Integer pageSize = simpleQuery.getPageSize();
        PageMethod.startPage(pageNum, pageSize);
        LambdaQueryWrapper<Simple> wrapper = new LambdaQueryWrapper<>();
        List<Simple> simpleList = simpleMapper.selectList(wrapper);
        List<SimpleVo> simpleVoList = MyModelUtil.copyCollectionTo(simpleList, SimpleVo.class);
        //获取分页信息
        long totalCount = 0L;
        if(simpleList instanceof Page){
            totalCount = ((Page<SimpleVo>) simpleVoList).getTotal();
        }

        Page<SimpleVo> resultPage = new Page<>();
        resultPage.addAll(simpleVoList);
        resultPage.setTotal(totalCount);
        resultPage.setPageNum(pageNum);
        resultPage.setPageSize(pageSize);
//        resultPage.setPages(totalPages);
        resultPage.setPages((int)totalCount % pageSize == 0 ? (int)totalCount / pageSize : (int)totalCount / pageSize + 1);
        return ResponseResult.success(resultPage);
    }

    @Override
    public ResponseResult<Void> updateSimple(SimpleDto dto) {
        Simple simple = BeanUtil.copyProperties(dto, Simple.class);
        Long userId = TokenData.takeFormRequest().getUserId();
        String userName = TokenData.takeFormRequest().getUserName();
        simple.setUpdateUserId(userId);
        simple.setUpdateUserName(userName);
        simple.setUpdateTime(new Date());
        int update = simpleMapper.updateById(simple);
        if(update == 0){
            return ResponseResult.error(ErrorCodeEnum.LOGIN_ERRROR);
        }
        return ResponseResult.success();
    }

    @Override
    public ResponseResult<Void> deleteSimple(List<Long> simpleIdList) {
        LambdaQueryWrapper<Simple> wrapper = new LambdaQueryWrapper<>();
        int delete = simpleMapper.delete(wrapper);
        if(delete == 0){
            return ResponseResult.error(ErrorCodeEnum.DATA_NOT_EXIST);
        }
        return ResponseResult.success();
    }
}
